package ebook.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import ebook.client.widgets.Widget1;
import ebook.shared.FieldVerifier;

public class T2epub implements EntryPoint {
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	private final EpubServiceAsync epubService = GWT.create(EpubService.class);
	private Widget1 widget1;

	public void onModuleLoad() {
		widget1 = new Widget1();
		final Label errorLabel = new Label();
		RootPanel.get("textFieldContainer").add(widget1);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		final Button downloadButton = new Button("Download");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		downloadButton.getElement().setId("downloadButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		final VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending text to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.add(downloadButton);
		HorizontalPanel a = new HorizontalPanel();
		dialogVPanel.add(a);
		a.add(closeButton);
		a.add(downloadButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				widget1.convertButton.setEnabled(true);
				widget1.convertButton.setFocus(true);
			}
		});

		// Create a handler for the convertButton and textField
		class MyHandler implements ClickHandler, KeyUpHandler {
			public void onClick(ClickEvent event) {
				send2Server();
			}

			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					send2Server();
				}
			}

			private void send2Server() {
				// First, we validate the input.
				errorLabel.setText("");
				String Title = widget1.Title.getText();
				String Chap = widget1.Chap.getText();
				String ContentOfChap = widget1.ContentOfChap.getHTML();
				if (!FieldVerifier.isValidName(Title, Chap, ContentOfChap)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				widget1.convertButton.setEnabled(false);
				textToServerLabel.setText(Title);
				serverResponseLabel.setText("");
				
				//Xử lý ContentOfChap
				
				epubService.greetServer(Title, Chap, ContentOfChap,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.setText("Remote Procedure Call - Failure");
								serverResponseLabel.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(final String blobkey) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(blobkey);
								dialogBox.center();
								closeButton.setFocus(true);
								downloadButton.addClickHandler(new ClickHandler() {
											public void onClick(ClickEvent event) {
												//Call to reader servlet
												Window.open("/download?blobkey="+ blobkey, widget1.Title.getText(), "");
											}
										});
							}
						});
			}
		}
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		widget1.convertButton.addClickHandler(handler);
		widget1.ContentOfChap.addKeyUpHandler(handler);
	}
}
